﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SAIM.Controllers
{
    public class ExceptionController : Controller
    {
        // GET: Exception/AccessDenied
        /// <summary>
        /// Access Denied Exception
        /// </summary>
        /// <returns></returns>
        [PreventDirectAccess]
        public ActionResult AccessDenied()
        {
            return View();
        }

        // GET: Exception/NotFound
        /// <summary>
        /// Page Not Found Exception
        /// </summary>
        /// <returns></returns>
        public ActionResult NotFound()
        {
            return View();
        }

        // GET: Exception/ServerError
        /// <summary>
        /// Internal Server Error Exception
        /// </summary>
        /// <returns></returns>
        [PreventDirectAccess]
        public ActionResult ServerError()
        {
            return View();
        }

        // GET: Exception/UnhandledError
        /// <summary>
        /// Unhandled Error Exception
        /// </summary>
        /// <param name="httpStatusCode"></param>
        /// <returns></returns>
        [PreventDirectAccess]
	    public ActionResult UnhandledError(int httpStatusCode)
	    {
            ViewBag.httpCode = httpStatusCode;
	        return View();
	    }

        private class PreventDirectAccessAttribute : FilterAttribute, IAuthorizationFilter
	    {
	        public void OnAuthorization(AuthorizationContext filterContext)
	        {
	            object value = filterContext.RouteData.Values["appErrorEvent"];
	            if (!(value is bool && (bool)value))
	                filterContext.Result = new ViewResult { ViewName = "NotFound" };
	        }
	    }
    }
}